<?php

namespace app\admin\controller;

use app\common\Helper;

use app\admin\model\Role;

use think\Db;
use think\Exception;
use think\Request;

class Operator extends Base
{

    public function getlist(){
        $params = $this->request->param();
        $where = [];
        if(isset($params['name']) && $params['name']){
            $where['name'] = ['like', "%{$params['name']}%"];
        }

        $model = model('Operator');
        $list = $model->where($where)->paginate(10);
        $total = $model->where($where)->count();

        return $this->fetch('',[
            'datas' => $list,
            'total' => $total,
            'formget' => $_GET
        ]);
    }

    public function add(){
        // 角色列表
        $roles = Role::where(['status' => 1])->order('sorting', 'ASC')->select();

        $this->assign('roles', $roles);

        return $this->fetch();
    }

    public function edit(Request $request){
        $param = $request->param();
        if(empty($param['id'])){
            return show(config('status.error_code'),'缺少参数','缺少参数');
        }
        $model = model('Operator');
        $info = $model->get($param['id']);
        $roleIds = db('roleuser')->where(['operatorId' => $param['id']])->field('roleId')->select();
        $info['roleIds'] = array_column($roleIds, 'roleId');

        // 角色列表
        $roles = Role::where(['status' => 1])->order('sorting', 'ASC')->select();

        $this->assign('roles', $roles);

        return $this->fetch('',[
            'info' => $info
        ]);
    }

    public function savePost(Request $request){
        if($request->isPost()){
            $param = $request->param();
            $model = model('Operator');

            if(empty($param['name'])){
                return show(config('status.error_code'),'缺少参数','缺少参数');
            }

            $data = $param;
            if($data['password']){
                $data['password'] = Helper::passwordEncrypt($data['password']);
            }else{
                unset($data['password']);
            }
            try {
                Db::startTrans();
                if(empty($param['id'])){
                    // 新增
                    $data['id'] = Helper::getUUID();
                    $data['createDate'] = date('Y-m-d H:i:s');
                    $data['createOper'] = $this->admin->id;

                    $result = $model->save($data);
                }else{
                    // 修改
                    $data['id'] = $param['id'];
                    $data['updateDate'] = date('Y-m-d H:i:s');
                    $data['updateOper'] = $this->admin->id;

                    $result = $model->allowField(true)->save($data, ['id' => $data['id']]);
                }

                if($result !== false){
                    $roleuserDB = db('roleuser');
                    $roleuserDB->where(['operatorId' => $data['id']])->delete();
                    foreach ($data['roleIds'] as $roleId) {
                        $roleuserData = [
                            'id' => Helper::getUUID(),
                            'roleId' => $roleId,
                            'operatorId' => $data['id']
                        ];

                        $res = $roleuserDB->insert($roleuserData);
                    }

                    if($res === false){
                        Db::rollback();
                        return show(config('status.error_code'),'保存失败','保存管理员角色失败');
                    }

                    Db::commit();
                    return show(config('status.success_code'),'保存成功','保存成功');
                }else{
                    Db::rollback();
                    return show(config('status.error_code'),'保存失败','保存失败');
                }
            } catch (Exception $e) {
                Db::rollback();
                return show(config('status.error_code'), '保存失败', $e->getMessage());
            }
        }else{
            return show(config('status.error_code'),'请求不合法','请求不合法');
        }
    }

    /**
     * 个人中心
     */
    public function profile(Request $request){
        $model = model('Operator');
        $info = $model->get($this->admin->id);

        return $this->fetch('',[
            'info' => $info
        ]);
    }

}